# -*- mode: makefile -*-
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#

#
# Copyright (c) 2014, Joyent, Inc.
#

#
# Makefile.node.defs: Makefile for building and bundling your own Node.js.
#
# NOTE: This makefile comes from the "eng" repo. It's designed to be dropped
# into other repos as-is without requiring any modifications. If you find
# yourself changing this file, you should instead update the original copy in
# eng.git and then update your repo to use the new version.
#

#
# This Makefile facilitates building and bundling your own copy of Node.js in
# your repo.  All it does is define variables for node, node-waf, and npm for
# you to use elsewhere in your Makefile and rules to build these tools when
# needed.
#
# To use this facility, include "Makefile.node.defs", use the variables as
# described below to define targets, and then include "Makefile.node.targ".
#
# There are two use cases addressed here:
#
# (1) Invoking node, node-waf, or npm as part of the build process, as in "npm
#     install" and "node-waf configure build".  To facilitate this, this
#     Makefile defines Make variables NODE, NODE_WAF, and NPM that you can use
#     to invoke these commands during the build process.  You MUST NOT assume
#     that these variables just evaluate to the filenames themselves, as they
#     may have environment variable definitions and other things that prevent
#     you from using them directly as a filename.  If you want that, see (2).
#
#     Wherever you use one of these variables, you MUST include a dependency on
#     the corresponding *_EXEC variable as well, like so:
#
#	node_modules/restify: deps/restify $(NPM_EXEC)
#		$(NPM) install deps/restify
#
#     or better, use an order-only dependency to avoid spurious rebuilds:
#
#	node_modules/restify: deps/restify | $(NPM_EXEC)
#		$(NPM) install deps/restify
#
#     Otherwise, the underlying file will not get built.  We don't
#     automatically build them as part of "all" because that approach is
#     brittle.
#
# (2) Specifying paths for invoking node, node-waf, or npm at RUNTIME, as in
#     specifying the path to node used for the start method of your service's
#     SMF manifest.  For this, this Makefile defines variables NODE_EXEC,
#     NODE_WAF_EXEC, and NPM_EXEC, which represent the relative paths of these
#     files from the root of the workspace.  You MUST NOT use these variables
#     to invoke these commands during the build process.  See (1) instead.
#
#     However, in order to work at runtime, you must build the tool as well.
#     That is, if you use NODE_EXEC to specify the path to node, you must
#     depend on NODE_EXEC somewhere. This usually happens anyway because you
#     usually need them during the build process too, but if you don't then
#     you need to explicitly add NODE_EXEC (or whichever) to your "all"
#     target.
#
# When including this Makefile, you MAY also specify:
#
#	BUILD			top-level directory for built binaries
#				(default: "build")
#
#	NODE_INSTALL		where node should install its built items
#				(default: "$BUILD/node")
#
#	NODE_CONFIG_FLAGS	extra flags to pass to Node's "configure"
#				(default: "--with-dtrace" on SmartOS; empty
#				otherwise.)
#

TOP ?= $(error You must include Makefile.defs before this makefile)

BUILD		?= build
NODE_INSTALL 	?= $(BUILD)/node
DISTCLEAN_FILES	+= $(NODE_INSTALL)

NODE_CONFIG_FLAGS += --prefix=$(TOP)/$(NODE_INSTALL)

ifeq ($(shell uname -s),SunOS)
	NODE_CONFIG_FLAGS += 	--with-dtrace \
				--openssl-libpath=/opt/local/lib \
				--openssl-includes=/opt/local/include
endif

NODE_EXEC	= $(NODE_INSTALL)/bin/node
NODE_WAF_EXEC	= $(NODE_INSTALL)/bin/node-waf
NPM_EXEC	= $(NODE_INSTALL)/bin/npm

# Ensure these use absolute paths to the executables to allow running
# from a dir other than the project top.
NODE		:= $(TOP)/$(NODE_EXEC)
NODE_WAF	:= $(TOP)/$(NODE_WAF_EXEC)
NPM		:= PATH=$(TOP)/$(NODE_INSTALL)/bin:$(PATH) node $(TOP)/$(NPM_EXEC)
